import $axios from '@/request';
import { MyAmapConsts } from '@/constant';

// send GET to amap to turn WGS84 coordination to amp coordination
export function getGps2AmapCoordination(longitude, latitude) {
    const params = {
        locations: callLonAndLatFormatter(longitude, latitude),
        coordsys: 'gps',
        key: MyAmapConsts.AMAP_TOKEN,
    }
    return $axios.get(MyAmapConsts.COORDINATION_CONVERT_GET, { params });
}

// send GET to amap to get static map
export async function getAmapStaticMap(longitude, latitude, zoomLevel) {
    const convertResult = await getGps2AmapCoordination(longitude, latitude);
    const locationInfo = callLonAndLatFormatterFromTemplateSingle(convertResult.locations);
    const params = {
        location: locationInfo,
        zoom: zoomLevel,
        size: '300*300',
        markers: `large,0x00bfff,这:${locationInfo}`,
        key: MyAmapConsts.AMAP_TOKEN,
    };
    return $axios.get(MyAmapConsts.STATIC_MAP_BASE_GET, {
        params,
        responseType: 'blob'
    });
}

function callLonAndLatFormatter(longitude, latitude) {
    const _longitude = longitude.toFixed(6);
    const _latitude = latitude.toFixed(6);
    return `${_longitude},${_latitude}`;
}

function callLonAndLatFormatterFromTemplateSingle(amapLocationStr) {
    var _longitude = 0.0;
    var _latitude = 0.0;
    const locations = amapLocationStr.split(",");
    if (locations.length > 1) {
        _longitude = parseFloat(locations[0]);
        _latitude = parseFloat(locations[1]);
    }
    return callLonAndLatFormatter(_longitude, _latitude);
}

// send GET to server to get complex and single model aside
export function getModelAsideData() {
    return $axios.get('/model/list');
}

// send GET to server to get replaced feature info
export function getFeatures2Replace(params = {
    containerId: '',
    substitutorId: ''
}) {
    return $axios.get('/model/feature/replace/list', { params });
}

// send GET to server to get FEATURE_FOCUS model's floor model
export function getFloorModel(params = {
    parentId: ''
}) {
    return $axios.get('/model/floor/get', { params });
}

// send GET to server to get floor and room aside
export function getFloorAndRoomAsideData(params = {
    parentId: ''
}) {
    return $axios.get('/model/floor/aside/list', { params });
}

// send GET to server tp get inner room model data
export function getInnerRoomModel(params = {
    parentId: ''
}) {
    return $axios.get('/model/room/inner/get', { params });
}

// send GET to server to get item aside data
// item info using another interface
export function getInteractiveItemAside(params = {
    innerRoomId: ''
}) {
    return $axios.get('/model/room/inner/item/list', { params });
}

// send GET to server to get interactive items in inner room model
export function getHighlightItemBatchIds(params = {
    innerRoomId: ''
}) {
    return $axios.get('/model/feature/inner/highlight/map', { params });
}

// send GET to server to get culture info when user clicking inner feature
export function getCultureInfoByTileFeature(params = {
    tileName: '',
    batchId: 0,
    innerRoomId: ''
}) {
    return $axios.get('/model/feature/inner/info/get', { params });
}

export function getRestoreFeature(params = {
    childId: ''
}) {
    return $axios.get('/back/surface', { params });
}

export function getSingleModelAsAside(params = {
    myId: ''
}) {
    return $axios.get('/back/single/get', { params });
}

export function getFloorWithEntrance(params = {
    parentId: '',
    roomId: ''
}) {
    return $axios.get('/back/floor/entrance', { params });
}